import React, { Component } from 'react'
import axios from 'axios';

export default class Search extends Component {


  search = () => {
    // 获取用户的输入（连续解构赋值+重命名）
    const { keyWordElement: {value: keyWord} } = this

    // 发送请求前通知App更新状态
    this.props.updateAppState({isFirst: false, isLoading: true})

    // 发送网络请求
    // https://api.github.com/search/users?q=xxxx
    // https://movie.douban.com/j/search_subjects?type=tv&tag=%E7%83%AD%E9%97%A8&page_limit=50&page_start=0
    // https://cn.bing.com/images/api/custom/search

    axios.get(`/api1/j/search_subjects?type=tv&tag=${encodeURI(keyWord)}&page_limit=50&page_start=0`).then((res) => {
      // 请求发送后通知App更新状态
      let {data:{subjects: users}} = res
      // this.props.updateAppState(users)
      this.props.updateAppState({ isLoading: false, users})
    }, err => {
      // 请求失败后通知App更新状态
      this.props.updateAppState({ isLoading: false, err })
    })
  }
  render() {
    return (
      <section className='jumbotron'>
        <h3 className='jumbotron-heading'>搜索github用户</h3>
        <div>
          <input ref={ c => this.keyWordElement = c } type="text" placeholder="输入关键词点击搜索" />
          <button onClick={this.search}>搜索</button>
        </div>
      </section>
    )
  }
}
